package cn.com.xf.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.domain.TsDict;
import cn.com.xf.domain.TsSiteinfo;
import cn.com.xf.domain.TsUser;
import cn.com.xf.service.CompanyService;
import cn.com.xf.web.form.LeaveForm;
import cn.com.xf.web.security.UserInfo;

@Controller
@RequestMapping("/company")
public class CompanyController extends AbstractBaseController {

	@Autowired
	private CompanyService companyService;
	
	/**
	 * 拥有权限站点
	 * @return
	 */
	@RequestMapping("/siteList")
	@ResponseBody
	public List<TsSiteinfo> directSiteList() {
		List<TsSiteinfo> siteList = companyService.selectDirectSites();
		List<TsSiteinfo> remainSiteList = new ArrayList<TsSiteinfo>();
		// 保留登陆用户所在站点，和获得管理权限的站点
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();

		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN")) {
				continue;
			}

			if (auth.contains("MANAGE_CHECK_")) {
				String siteId = auth.substring(13, 19);
				if (!remainList.contains(siteId)) {
					remainList.add(siteId);
				}
			}
		}

		for (TsSiteinfo siteInfo : siteList) {
			for (String siteId : remainList) {
				if (siteId.equalsIgnoreCase(siteInfo.getSiteid())) {
					remainSiteList.add(siteInfo);
				}
			}
		}

		return remainSiteList;
	}
	
	
	/**
	 * 所有站点
	 * @return
	 */
	@RequestMapping("/sites")
	@ResponseBody
	public List<TsSiteinfo> Sites() {
		List<TsSiteinfo> list = companyService.selectSites();
		return list ;
	}

	@RequestMapping("/siteListWithCarline")
	@ResponseBody
	public List<TsSiteinfo> directSiteListWithCarline() {
		List<TsSiteinfo> siteList = companyService.selectDirectSites();
		List<TsSiteinfo> remainSiteList = new ArrayList<TsSiteinfo>();
		// 保留登陆用户所在站点，和获得管理权限的站点
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		boolean hasHumen = false;
		boolean hasShenzhen = false;
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN")) {
				hasHumen = true;
				continue;
			}
			if (auth.equals("MANAGE_CHECK_SHENZHEN")) {
				hasShenzhen = true;
				continue;
			}
			if (auth.contains("MANAGE_CHECK_")) {
				String siteId = auth.substring(13, 19);
				if (!remainList.contains(siteId)) {
					remainList.add(siteId);
				}
			}
		}

		for (TsSiteinfo siteInfo : siteList) {
			for (String siteId : remainList) {
				if (siteId.equalsIgnoreCase(siteInfo.getSiteid())) {
					remainSiteList.add(siteInfo);
				}
			}
		}
		
		if (hasHumen) {
			TsSiteinfo humen = new TsSiteinfo();
			humen.setSiteid("HUMEN");
			humen.setSitename("虎门仓车线");
			remainSiteList.add(humen);
		}
		if (hasShenzhen) {
			TsSiteinfo shenzhen = new TsSiteinfo();
			shenzhen.setSiteid("SHENZHEN");
			shenzhen.setSitename("深圳仓车线");
			remainSiteList.add(shenzhen);
		}
		return remainSiteList;
	}
	
	@RequestMapping("/deptList")
	@ResponseBody
	public List<TsDict> deptList() {
		return companyService.selectDepts();
	}
	
	@RequestMapping("/deviceList")
	@ResponseBody
	public List<Map<String, Object>> getDevices() {
		return companyService.getDevices();
	}

	@RequestMapping("/userList")
	@ResponseBody
	public List<TsUser> userList() {
		List<TsSiteinfo> siteList = companyService.selectDirectSites();
		List<TsSiteinfo> remainSiteList = new ArrayList<TsSiteinfo>();
		// 保留登陆用户所在站点，和获得管理权限的站点
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN"))
				continue;
			if (auth.contains("MANAGE_CHECK_")) {
				String siteId = auth.substring(13, 19);
				if (!remainList.contains(siteId)) {
					remainList.add(siteId);
				}
			}
		}
		for (TsSiteinfo siteInfo : siteList) {
			for (String siteId : remainList) {
				if (siteId.equalsIgnoreCase(siteInfo.getSiteid())) {
					remainSiteList.add(siteInfo);
				}
			}
		}
		if (!CollectionUtils.isEmpty(remainSiteList)) {
			return companyService.selectUsers(remainSiteList.get(0).getSiteid());
		}
		return null;
	}

	@RequestMapping("/userListBysiteId")
	@ResponseBody
	public List<TsUser> userListBysiteId(HttpServletRequest request, HttpServletResponse response, LeaveForm form) {
		String siteId = form.getSiteid();
		List<TsSiteinfo> siteList = companyService.selectDirectSites();
		List<TsSiteinfo> remainSiteList = new ArrayList<TsSiteinfo>();
		// 保留登陆用户所在站点，和获得管理权限的站点
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN"))
				continue;
			if (auth.contains("MANAGE_CHECK_")) {
				String s = auth.substring(13, 19);
				if (!remainList.contains(s)) {
					remainList.add(s);
				}
			}
		}
		if (!CollectionUtils.isEmpty(remainList) && remainList.contains(siteId)) {
			return companyService.selectUsers(siteId);
		}
		return null;
	}
	
	@RequestMapping("/deptsAndUsers")
	@ResponseBody
	public List<Map<String, Object>> getDeptsAndUsers(HttpServletRequest request, HttpServletResponse response, LeaveForm form) {
		String siteId = form.getSiteid();
		// 保留登陆用户所在站点，和获得管理权限的站点
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN")) {
				remainList.add("HUMEN");
				continue;
			}
			if (auth.equals("MANAGE_CHECK_SHENZHEN")) {
				remainList.add("SHENZHEN");
				continue;
			}
			if (auth.contains("MANAGE_CHECK_")) {
				String s = auth.substring(13, 19);
				if (!remainList.contains(s)) {
					remainList.add(s);
				}
			}
		}
		if (!CollectionUtils.isEmpty(remainList) && remainList.contains(siteId)) {
			return companyService.getDeptsAndUsersWithCarline(siteId);
		}
		return null;
	}
	
	@RequestMapping("/datagridDepts")
	@ResponseBody
	public List<Map<String, Object>> datagridDepts(HttpServletRequest request, HttpServletResponse response, LeaveForm form) {
		String siteId = form.getSiteid();
		// 保留登陆用户所在站点，和获得管理权限的站点
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN"))
				continue;
			if (auth.contains("MANAGE_CHECK_")) {
				String s = auth.substring(13, 19);
				if (!remainList.contains(s)) {
					remainList.add(s);
				}
			}
		}
		if (!CollectionUtils.isEmpty(remainList) && remainList.contains(siteId)) {
			return companyService.datagridDepts(siteId);
		}
		return null;
	}
	@RequestMapping("/datagridUsers")
	@ResponseBody
	public List<Map<String, Object>> datagridUser(HttpServletRequest request, HttpServletResponse response, LeaveForm form) {
		String siteId = form.getSiteid();
		String deptId = form.getDeptid();
		// 保留登陆用户所在站点，和获得管理权限的站点
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		ArrayList<String> remainList = new ArrayList<String>();
		if (!"769000".equalsIgnoreCase(user.getSiteId())) {
			remainList.add(user.getSiteId());
		}
		Object auths[] = SecurityContextHolder.getContext().getAuthentication().getAuthorities().toArray();
		for (Object o : auths) {
			String auth = o + "";
			if (auth.equals("MANAGE_CHECK_HUMEN") || auth.equals("MANAGE_CHECK_SHENZHEN"))
				continue;
			if (auth.contains("MANAGE_CHECK_")) {
				String s = auth.substring(13, 19);
				if (!remainList.contains(s)) {
					remainList.add(s);
				}
			}
		}
		if (!CollectionUtils.isEmpty(remainList) && remainList.contains(siteId)) {
			return companyService.datagridUsers(siteId,deptId);
		}
		return null;
	}

}
